package com.xiaoge.controller;

import com.xiaoge.entity.SysUser;
import com.xiaoge.auth.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
     * @author 1
     */
    @Controller
    public class SecurityController {


        /**
         * 主页面
         * @return
         */
        @GetMapping("/home")
        public String home(){
            return "homepage";
        }

        /**
         * 用户管理
         * @return
         */
        @GetMapping("/users")
        public String users(){
            return "users";
        }

        /**
         * 角色管理
         * @return
         */
        @GetMapping("/roles")
        public String roles(){
            return "roles";
        }

        /**
         * 菜单管理
         * @return
         */
        @GetMapping("/menus")
        public String menus(){
            return "menus";
        }

        /**
         * 其他管理
         * @return
         */
        @GetMapping("/others")
        public String others(){
            return "others";
        }

        @GetMapping("/error")
        public String error(){
            return "error";
        }

    @RequestMapping("/login/error")
    public void loginError(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("text/html;charset=utf-8");
        //`AuthenticationException` 是spring security的所有父类
        AuthenticationException exception =
                (AuthenticationException)request.getSession().getAttribute("SPRING_SECURITY_LAST_EXCEPTION");
        try {
            response.getWriter().write(exception.toString());
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
}
